// Two function need to be implemented , GameMapInfo_handler & GameMapInfo_handler_configMode
// used GM Variables
// ds_huntRuleCurrent : current hunt rule object .

function GameMapInfo_handler(){
    if(GM_safeVariables["ds_huntRuleCurrent"] != null && GM_safeVariables["ds_huntRuleCurrent"] != ''){
        var lis = $x("//div[@class='cz_info']/ul")[0].getElementsByTagName("li");
        if(lis[0].innerHTML.indexOf("狩獵次數") >= 0){ //the place has been visited,so re-select the target place
            log("當前地點已經被狩獵過,重新到地圖選擇其他地點...");
            clickLink("/GameMap");
            return;
        }
        var link = "";
        var links = $x("//div[@class='col1']")[0].getElementsByTagName('a');
        var currentHuntRule = GM_safeVariables["ds_huntRuleCurrent"];
        currentHuntRule = eval('('+currentHuntRule+')');
        var strHeroName = '';
        for(var m=0;m<currentHuntRule.heros.length;m++){
            var objHero = currentHuntRule.heros[m];
            strHeroName += objHero.name + ',';
        }
        log("按照規則設定,為<strong>"+strHeroName+"</strong>選擇預定的攻擊方式.");
        for(var i=0;i<links.length;i++){
            if(currentHuntRule != null && currentHuntRule != '' && currentHuntRule.target == 'wild' && currentHuntRule.attackType == 'collect'){
                if(links[i].innerHTML == '採集'){
                    link = links[i].getAttribute('href');
                    break;
                }
            }else{
                if(links[i].innerHTML == '狩獵' || links[i].innerHTML == '出軍' || links[i].innerHTML == '訪問'){
                    link = links[i].getAttribute('href');
                    break;
                }
            }
        }
        if(link != "")
        	clickLink(link);
        else{
            log("地圖信息頁無法找到任何可操作的鏈接,跳轉到首頁...");
            clickLink("/mindex");
        }
    }else{
        log("您沒有定制任何狩獵規則,自動跳轉回首頁...");
        clickLink('/mindex');
        return;
    }

}

function GameMapInfo_handler_configMode(){
	$("img.cz").after('<div style="position:absolute;left:150px;top:300px;"><ul id="addHere"></ul></div>');
	for(var i = 0 ; i<CONFIG.attackList.length;i++){
		var attackCity = CONFIG.attackList[i];
		var id = attackCity.urls.split('mapId=')[1];
		var url = '/GameMapInfo?mapId=' + id;
		var mapUrl = '/GameMap?mapId=' + id;
		$("#addHere").append("<li class='delLi' id='"+attackCity.id+"'><a class='delThisLink' aid='"+attackCity.id+"' href='#'><img src='/sys/images/del.gif'></a>&nbsp;&nbsp;[<a href='"+mapUrl+"'>地图</a>] &nbsp;&nbsp;<a href='"+url+"'>"+attackCity.names+"</a>");
	}
	//make judgement,just add link for user castle
	if(!($("li:contains('人口')").length > 0 && $("li:contains('種族')").length > 0)){
		return;
	}
	$("div.col1 > div.cz_info > ul").append("<li>"+getDSIconPath()+"<a class=\"ds_cfg_addAttackCity\" href=\"#\">添加为持续打击目标</a></li>");
	//$("a:contains('出军')").after('<a class="ds_cfg_addAttackCity" href="#">将该城堡添加为可供攻击的目标</a>');
	$(".ds_cfg_addAttackCity").click(function(){
		var attackCity = new Object();
    	var id = new Date().getTime();
		attackCity.id = id;
		attackCity.urls = $("a:contains('出軍')").attr('href');
		var names = $("div.cz_info>h1")[0].innerHTML;
		names = names.replace("<span>","").replace("</span>","");
		attackCity.names = names;
		CONFIG.attackList.push(attackCity);
		var id = attackCity.urls.split('mapId=')[1];
		var url = '/GameMapInfo?mapId=' + id;
		var mapUrl = '/GameMap?mapId=' + id;
		$("#addHere").append("<li class='delLi' id='"+attackCity.id+"'><a class='delThisLink' aid='"+attackCity.id+"' href='#'><img src='/sys/images/del.gif'></a>&nbsp;&nbsp;[<a href='"+mapUrl+"'>地圖</a>] &nbsp;&nbsp;<a href='"+url+"'>"+attackCity.names+"</a>");
	  	$(".delThisLink").click(function(){
	  		var id = $(this).attr('aid');
	  		var lis = $("li#"+id).remove();
	    	var rmIdx = -1;
	    	for(var i=0;i<CONFIG.attackList.length;i++){
	      		if(CONFIG.attackList[i].id == id){
	        		rmIdx = i;
	        		break;
	      		}
	    	}
	    	if(rmIdx != -1)
	      		CONFIG.attackList.remove(rmIdx);
	  	})
	})
  	$(".delThisLink").click(function(){
  		var id = $(this).attr('aid');
  		var lis = $("li#"+id).remove();
    	var rmIdx = -1;
    	for(var i=0;i<CONFIG.attackList.length;i++){
      		if(CONFIG.attackList[i].id == id){
        		rmIdx = i;
        		break;
      		}
    	}
    	if(rmIdx != -1)
      		CONFIG.attackList.remove(rmIdx);
  	})

}